#ifndef __MEDIA_FILE_DB_MANAGER__
#define __MEDIA_FILE_DB_MANAGER__
#include <future>
#include <sqlite_orm/sqlite_orm.h>
#include <mutex>
#include <condition_variable>

using namespace sqlite_orm;

const std::string SqliteDbName = "media_file.db";

struct MediaFile{
    int m_id;
    std::string m_room_id;
    std::string m_file_path;
    std::string m_channel;
    uint32_t m_start_time;
    uint32_t m_stop_time;
    uint32_t m_file_size;
    bool m_has_sync;	
};


class MediaFileDbManager{
public:
    using Ptr = std::shared_ptr<MediaFileDbManager>;
    static Ptr Instance();
    MediaFileDbManager();
    ~MediaFileDbManager();
    bool Init(const std::string& db_file_path);
    bool Reset();
    bool AddFileEntry(const MediaFile& file_entry);
    bool ReplaceFileEntry(const MediaFile& file_entry);

private:
    void fileSyncToServerTask();
    void clearFile();
    using StorageType = decltype(make_table("MediaFile",
                            make_column("id", &MediaFile::m_id, primary_key()),
                            make_column("room_id", &MediaFile::m_room_id),
                            make_column("file_path", &MediaFile::m_file_path),
                            make_column("channel", &MediaFile::m_channel), 
                            make_column("start_time", &MediaFile::m_start_time),
                            make_column("stop_time", &MediaFile::m_stop_time),
                            make_column("file_size", &MediaFile::m_file_size),
                            make_column("has_sync", &MediaFile::m_has_sync)));
    std::shared_ptr<internal::storage_t<StorageType>> m_storager_ptr;
    std::future<void> m_sync_task;
    bool m_stopped;
    std::mutex m_mtx;
    std::condition_variable m_con_var;
    std::string m_storage_path;

};


#endif